package rf.conctrol;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.RowSet;
import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl;


import rf.db.DBconnection;

public class DBhander {
	
	public RowSet openSql(String sql){
		//连接实例
		Connection conn = new DBconnection().getConnection();
		//ssss
		Statement st = null;
		//��װ���
		ResultSet rs = null;
		CachedRowSet  rowset =null;
		
		try {
			st = conn.createStatement();
			rs =  st.executeQuery(sql);
			rowset = new CachedRowSetImpl();
			rowset.populate(rs);

		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		this.closeDB(conn,st,rs);
		
		
		
		return rowset;
	}
	
	/**
	 * ���� ɾ�� �޸�
	 */
	public void execSql(String sql){
		//�������
		Connection conn = new DBconnection().getConnection();
		//����ִ��
		Statement st = null;
		
		try {
			st = conn.createStatement();
			st.execute(sql);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		this.closeDB(conn, st);
	}
	
	/**
	 * ���� ɾ�� �޸�
	 */
	public void execSql(List<String> list){
		//�������
		Connection conn = new DBconnection().getConnection();
		//����ִ��
		Statement st = null;
		
		try {
			st = conn.createStatement();
			for(String sql: list){
				st.execute(sql);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * �رո�������
	 * @param conn
	 * @param st
	 * @param rs
	 */
	public void  closeDB(Connection conn,Statement st,ResultSet rs){
		try {
			rs.close();
			st.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void  closeDB(Connection conn,Statement st){
		try {
			st.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws Exception {
		DBhander db = new DBhander();
		List<String> list= new ArrayList<String>();
		String sql ="delete from test where id=1";
		list.add(sql);
		list.add("update test set age=22 where id = 3");
		/*RowSet rs = db.openSql("select * from test");
		while(rs.next()){
			System.out.println(rs.getString("name"));
		}*/
		db.execSql(list);
	}
}
